<?php
namespace sm_shop_plugin_diy\api;
use sm_shop\controller;
use sm_shop\model\tool\imageModel;
use sm_shop\model\settingModel;
use sm_shop_plugin_diy\model\diyModel;

class index extends controller{

    private $diy_id = 0;
    public function index(){



    }

    public function single(){

        global $_GPC;
        $res = [
            'status'=>0
        ];
        $this->diy_id = $_GPC['diy_id'] ?? $this->diy_id;
        $res['diy_id'] = $this->diy_id;
        if( empty( $this->diy_id ) ){

            $res['status']=1;
            $res['description'] = '请求不合法';
            echo json_encode( $res );
            die();
        }

        $diy = diyModel::single( $this->diy_id );
        $diy_arr = json_decode( $diy['diy'], true );
        foreach( $diy_arr as $key => &$item ){

            if( strpos($item['layout'],'image.')!==false ){
                foreach( $item['cubes'] as $i=>&$cube ){
                    $width = $cube['width'];
                    $height = $cube['height'];
                    $cube['image'] = str_replace('/attachment/','', $cube['image']);
//                    $cube['image'] = imageModel::resize2( $cube['image'], $width, $height );
                    $cube['image'] =  tomedia( $cube['image'] );
                    $this->setLink( $cube );


                }

            }
            if( $item['layout'] == 'lunbo' ){
                $width = $item['width']??750;
                $height = $item['height']??360;
                foreach( $item['cubes'] as $i => &$cube ){

                    $cube['image'] = str_replace('/attachment/','', $cube['image']);
                    $cube['image'] =  tomedia( $cube['image'] );
//                    $cube['image'] = imageModel::resize2( $cube['image'], $width, $height );
                    $this->setLink( $cube );
                }
            }


        }
//        $res['diy'] = json_decode( $diy['diy'], true );;
        echo json_encode( $diy_arr );

    }

    public function home(){

        $this->diy_id = settingModel::getItem( 'diy', 'home' );
        $this->single( );

    }

    private function setLink( &$cube ){
        $page_link = $cube['page_link'];
        $key = $page_link['key'];
        if( $key == 'product' ){
            $cube['openType']='navigate';
            $cube['link'] = '/sm_shop/pages/product/product';
            if( !empty($page_link['params']) ){
                $cube['link'] .= '?product_id=' . $page_link['params']['product_id'];
            }else{
                $cube['link'] = '/sm_shop/pages/home/home';
            }
        }
        if( $key == 'home' ){
            $cube['openType']='switchTab';
            $cube['link'] = '/sm_shop/pages/home/home';
        }
        if( $key == 'cart' ){
            $cube['openType']='switchTab';
            $cube['link'] = '/sm_shop/pages/cart/cart';
        }
        if( $key == 'user' ){
            $cube['openType']='switchTab';
            $cube['link'] = '/sm_shop/pages/user/user';
        }
        if( $key == 'order' ){
            $cube['openType']='navigate';
            $cube['link'] = '/sm_shop/pages/order/list';
        }
        if( $key == 'category' ){
            $cube['openType']='switchTab';
            $cube['link'] = '/sm_shop/pages/category/category';
            $link_params = [];
            if( !empty( $page_link['params']['brand_id'] ) ){
                $link_params[] = 'brand_id=' . $page_link['params']['brand_id'];
            }
            if( !empty( $page_link['params']['category_id'] ) ){
                $link_params[] = 'category_id=' . $page_link['params']['category_id'];
            }
            if( !empty( $link_params ) ){
                $cube['link'] .= '?' . explode('&', $link_params );
            }
        }

    }


}